﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace XMLData
{
    public class XMLData
    {
        XmlDocument doc;
        XmlReader reader;
        string path;
        string filename;
        string rootName;

        public XMLData()
        {
            doc = new XmlDocument();
            path = Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), "Data.xml");
            reader = XmlReader.Create(path);
            doc.Load(reader);
            reader.Close();
        }
        public XMLData(string path, string filename, string rootName = "Data")
        {
            this.path = path;
            this.filename = filename;
            this.rootName = rootName;

            doc = new XmlDocument();
            reader = XmlReader.Create(path);
            doc.Load(reader);
            reader.Close();
        }
        public XMLData(string fileContent, string rootName = "Data")
        {
            doc = new XmlDocument();
            doc.LoadXml(fileContent);
            this.rootName = rootName;
        }
        public string XmlPath
        {
            get { return this.path; }
            set
            {
                this.path = value;
                reader = XmlReader.Create(path);
                doc.Load(reader);
                reader.Close();
            }
        }
        public string GetDato(string branch, string parametro)
        {
            String valor = "";
            XmlNode nodo = doc.SelectSingleNode("/" + this.rootName + "/" + branch);
            if (nodo != null)
            {
                if (nodo[parametro] != null)
                {
                    valor = nodo[parametro].Attributes["valor"].Value;
                }
                else
                {
                    valor = "";

                }
            }
            else
            {
                valor = "";
            }
            valor.Trim();
            return valor;
        }
        public void SetDato(string branch, string parametro, string valor)
        {
            XmlNode root = doc.DocumentElement;
            XmlNode nodo = doc.SelectSingleNode(root.Name + "/" + branch);
            if (nodo == null)
            {
                XmlElement elem = doc.CreateElement(branch);
                root.AppendChild(elem);
                elem = doc.CreateElement(parametro);
                XmlAttribute attr = doc.CreateAttribute("valor");
                attr.Value = valor;
                elem.Attributes.Append(attr);
                nodo = doc.SelectSingleNode(root.Name + "/" + branch);
                nodo.AppendChild(elem);

            }
            else
            {
                if (nodo[parametro] != null)
                {
                    nodo[parametro].Attributes["valor"].Value = valor;
                }
                else
                {
                    XmlElement elem = doc.CreateElement(parametro);
                    XmlAttribute attr = doc.CreateAttribute("valor");
                    attr.Value = valor;
                    elem.Attributes.Append(attr);
                    nodo = doc.SelectSingleNode(root.Name + "/" + branch);
                    nodo.AppendChild(elem);
                }

            }
            doc.Save(filename);
        }
    }
}
